package com.example.config;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        // 设置HTTP状态码
        // 可以自定义返回的消息
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println("{\"code\":403, \"message\":\"没有权限访问该资源\"}");
    }
}